"""
原系统API和菜单相关数据模型
"""

from tortoise import fields
from app.models.base.base import BaseModel
from enum import Enum

class MenuType(str, Enum):
    """菜单类型枚举"""
    MENU = "menu"  # 菜单
    BUTTON = "button"  # 按钮
    INTERFACE = "interface"  # 接口


class Menu(BaseModel):
    """菜单模型"""
    name = fields.CharField(max_length=20, description="菜单名称", index=True)
    remark = fields.JSONField(null=True, description="保留字段")
    menu_type = fields.CharEnumField(MenuType, null=True, description="菜单类型")
    icon = fields.CharField(max_length=100, null=True, description="菜单图标")
    path = fields.CharField(max_length=100, description="菜单路径", index=True)
    order = fields.IntField(default=0, description="排序", index=True)
    parent_id = fields.IntField(default=0, description="父菜单ID", index=True)
    is_hidden = fields.BooleanField(default=False, description="是否隐藏")
    component = fields.CharField(max_length=100, description="组件")
    keepalive = fields.BooleanField(default=True, description="存活")
    redirect = fields.CharField(max_length=100, null=True, description="重定向")

    class Meta:
        table = "menu"
        table_description = "菜单表"
        
    def __str__(self):
        return f"Menu(id={self.id}, name={self.name}, path={self.path})"
    
    async def get_children(self):
        """获取子菜单"""
        return await Menu.filter(parent_id=self.id, is_deleted=False).order_by('order')
    
    async def get_parent(self):
        """获取父菜单"""
        if self.parent_id > 0:
            return await Menu.filter(id=self.parent_id).first()
        return None